using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;

namespace IQIGame.Onigao.GamePlay
{
    public class LocalLogicClientSession : ISession
    {
        public IExecutor GetExecutor()
        {
            //因为客户端发消息是在主线程同步序列化的, 所以这里直接执行action
            return DefaultSessionExecutor.Instance;
        }

        public bool SendPack(SendPackage pack)
        {
            ReceivePackage receivePackage = ConcurrentClassPool<ReceivePackage>.Get();
            try
            {
                pack.FinishWrite();
                int size = pack.OutputStream.Size;
                byte[] data = pack.OutputStream.Buf;
                receivePackage.Init(data, 0, size);

                LocalLogicServerSession localLogicServerSession = LocalLogicManager.Instance.localLogicServerSession;
                IReceiveMessage messageRequest = LocalLogicServerNetHandler.Instance.NewRequest(receivePackage);
                if (messageRequest == null)
                {
                    return false;
                }
                return LocalLogicServerNetHandler.Instance.ProcessPackage(localLogicServerSession, messageRequest);
            }
            finally
            {
                ConcurrentClassPool<SendPackage>.Put(pack);
                ConcurrentClassPool<ReceivePackage>.Put(receivePackage);
            }
        }

        public void Close(bool active = false)
        {
        }

        public void Disconnect()
        {
        }

    }
}
